package com.nari.ysphd.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;

public class ConnectionChangeReceiver extends BroadcastReceiver {

	private Handler mHandler;

	private static boolean connectFlag = true;

	public ConnectionChangeReceiver(Handler handler) {
		this.mHandler = handler;
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null || !networkInfo.isAvailable()) {
			mHandler.sendEmptyMessage(0);
			connectFlag = false;
		} else {
			if (!connectFlag) {
				mHandler.sendEmptyMessage(1);
				connectFlag = true;
			}
		}
	}
}